package com.zyd.shiro.controller.admin;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.persistence.beans.WxappHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.UserExamine;
import com.zyd.shiro.business.service.MallUserExamineService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/userExamine")
public class UserExamineController {

	@Autowired
	private MallUserExamineService userExamineService;
	
	/**
	 * 查询全部数据
	 * @return
	 *
	 */
	@RequestMapping(value="/",method=RequestMethod.GET)
	public ResponseVO findAll(){
		 return ResultUtil.success("查询成功",userExamineService.findAll());
		
	}
	
	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="findOneById/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", userExamineService.findOne(id));	
		
	}
	/**
	 * 获得图片显示图片网址
	 * @
	 * @
	 */
	@RequestMapping(value="/getImage/{id}",method=RequestMethod.GET)
	public ResponseVO getImage(@PathVariable Integer id){
		UserExamine userExamine = userExamineService.findOne(id);
		try {
			return ResultUtil.success("查询成功", userExamineService.downloadTest(userExamine.getImage()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return ResultUtil.error("未找到图片");
		}
	}

	/**
	 * 根据用户id查询
	 * @param user_id
	 * @return
	 */
    @RequestMapping(value="/findByuserId/{user_id}",method=RequestMethod.GET)
    public ResponseVO findOneByUserID(@PathVariable Integer user_id){
        return ResultUtil.success("查询成功", userExamineService.findAllByUser_id(user_id));

    }

	/**
	 * 根据状态信息查询
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public ResponseVO findByState(@PathVariable Integer state){
		return ResultUtil.success("查询成功",userExamineService.findAllByState(state));
	}

	/**
	 * 查找未审核信息列表
	 * @return
	 */
	@RequestMapping(value = "/nolist/",method = RequestMethod.GET)
	public ResponseVO nolist(){
		return ResultUtil.success("查询成功",userExamineService.findAllByState(0));
	}

	/**
	 * 查询审查通过列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/successlist/",method = RequestMethod.GET)
	public ResponseVO successlist(){
		return ResultUtil.success("查询成功",userExamineService.findAllByState(1));
	}


	/**
	 * 查询审查失败列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/faillist/",method = RequestMethod.GET)
	public ResponseVO faillist(){
		return ResultUtil.success("查询成功",userExamineService.findAllByState(2));
	}


	@RequestMapping(value = "/examSuccess/{id}",method = RequestMethod.PUT)
	public ResponseVO examSuccess(@PathVariable Integer id){
		UserExamine userExamine = userExamineService.findOne(id);
		userExamine.setState(1);
		userExamineService.update(userExamine);
		return ResultUtil.success("修改成功");
	}

	@RequestMapping(value = "/examFault/{id}/{re}",method = RequestMethod.PUT)
	public ResponseVO examFault(@PathVariable Integer id,@PathVariable String re){
		UserExamine userExamine = userExamineService.findOne(id);
		userExamine.setState(2);
		userExamine.setReasion(re);
		userExamineService.update(userExamine);
		return ResultUtil.success("修改成功");
	}

	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}/{type}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size,@PathVariable int type){
		Page<UserExamine> pageList = userExamineService.findPage(page, size,type);
		return ResultUtil.success("查询成功",new PageResult(pageList.getTotalElements(), pageList.getContent() ) );	
	}
	
	/**
	 * 分页+多条件查询 
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}/{searchMap}",method=RequestMethod.POST)
	public ResponseVO findSearch(@PathVariable int searchMap ,@PathVariable int page,@PathVariable int size){
		Page<UserExamine> pageList = userExamineService.findSearch(searchMap, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );		
	}
	
	/**
	 * 增加
	 * @param userExaminess
	 */
	@RequestMapping(value="/",method=RequestMethod.POST)
	public ResponseVO add(@RequestBody UserExamine userExamine  ){
		userExamineService.add(userExamine);			
		return ResultUtil.success("增加成功");
	}
	
	/**
	 * 修改
	 * @param userExamine
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.PUT)
	public ResponseVO update(@RequestBody UserExamine userExamine,@PathVariable Integer id ){
		userExamine.setId(id);
		userExamineService.update(userExamine);		
		return ResultUtil.success("修改成功");
	}
	
	/**
	 * 删除
	 * @param id
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
	public ResponseVO delete(@PathVariable Integer id ){
		userExamineService.delete(id);
		return ResultUtil.success("删除成功");
	}
	
}
